T005 自定义控件 可配置加载 ImageView 发表于 2019-07-18 | 分类于 Custom View | 原理很简单,使一张图围绕自己的中心点匀速旋转。完整代码如下:1234567891011121314151617181920212223242526272829303132333435363738package com.xxt.xtest;import android.content.Context;import android.util.AttributeSet;import android.view.animation.Animation;import android.view.animation.LinearInterpolator;import android.view.animation.RotateAnimation;import androidx.annotation.Nullable;import androidx.appcompat.widget.AppCompatImageView;public class LoadingImageView extends AppCompatImageView { public LoadingImageView(Context context) { super(context); init(); } public LoadingImageView(Context context, @Nullable AttributeSet attrs) { super(context, attrs); init(); } public LoadingImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(); } private void init() { RotateAnimation rotateAnim = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); rotateAnim.setRepeatCount(Animation.INFINITE); rotateAnim.setDuration(1000); rotateAnim.setInterpolator(new LinearInterpolator()); this.startAnimation(rotateAnim); }} 使用方式:1234567891011121314<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <com.xxt.xtest.LoadingImageView android:layout_width="50dp" android:layout_height="50dp" android:layout_margin="50dp" android:src="@drawable/taiji"/></LinearLayout>